package com.shuqi.activity.viewport;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.shuqi.controller.R;

/***
 * <b>底部弹出 Toast消息，自动隐藏</b> <br>
 * 使用方式：BottomToast.makeToast(activity,"联网成功").show();
 * 
 * @yanghuawei
 */
public class BottomToastNight {
    /** 消息：隐藏消息 */
    private final int MSG_HIDE_TOAST = 100;

    /** 默认隐藏消息延时 */
    private final int defaultDelay = 3000;
    /** 隐藏消息延时 */
    private long delayMillis = defaultDelay;

    private static BottomDialog bottomDialog;

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == MSG_HIDE_TOAST) {
                hideToast();
            }
        };
    };

    private BottomToastNight(Context activity, String msg, int resourceId, int time) {
        View view = View.inflate(activity, R.layout.view_toast_bottom_night, null);
        bottomDialog = new BottomDialog(activity, view);
        TextView tv = (TextView) view.findViewById(R.id.view_bottomtoast_msg);
        tv.setText(msg);
        tv.setCompoundDrawablesWithIntrinsicBounds(resourceId, 0, 0, 0);
    }

    /** 显示Toast */
    public void show() {
        if (bottomDialog != null) {
            bottomDialog.show();
            handler.removeMessages(MSG_HIDE_TOAST);
            handler.sendEmptyMessageDelayed(MSG_HIDE_TOAST, delayMillis);
        }
    }

    /***
     * 显示Toast
     * 
     * @param isAutoHide 是否自动消失
     */
    public void show(boolean isAutoHide) {
        if (bottomDialog != null) {
            bottomDialog.show();
            if (isAutoHide) {
                handler.sendEmptyMessage(MSG_HIDE_TOAST);
            }
        }
    }

    /** 手动消失Toast */
    public void hideToast() {
        if (bottomDialog != null && bottomDialog.isShowing()) {
            bottomDialog.dismiss();
        }
    }

    /** 设置消息自动消失的延时时间：毫秒 */
    public void setHidedelayMillis(long delayMillis) {
        if (delayMillis >= 0) {
            this.delayMillis = delayMillis;
        }
    }

    /**
     * 底部弹出的消息：会自动隐藏
     * 
     * @param context
     * @param msg 提示语
     * @param resourceId 提示语左边的小图标
     * @return
     */
    public static BottomToastNight makeToast(Context context, String msg, int resourceId) {
        BottomToastNight toast = new BottomToastNight(context, msg, resourceId, 3000);
        return toast;
    }

    /**
     * 底部弹出的消息：会自动隐藏
     * 
     * @param context
     * @param msg 提示语
     * @return
     */
    public static BottomToastNight makeToast(Context context, String msg) {
        BottomToastNight toast = new BottomToastNight(context, msg, 0, 3000);
        return toast;
    }

}
